//
//  CYOrderDetailViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/27.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYOrderDetailViewController.h"
#import "CYDataSourceDelegate.h"
#import "CYOrderDetailCell.h"
#import "CYSuccessTipsView.h"
@interface CYOrderDetailViewController (){
    CYDataSource *_datasource;
    CYDelegate *_delegate;
}
@property (nonatomic, strong) CYLabel *orderNum;//注单号
@property (nonatomic, strong) CYButton *revokeBtn;//撤销订单按钮
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSDictionary *data;
@end

@implementation CYOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

/*懒加载*/
- (CYLabel *)orderNum{
    if (!_orderNum) {
        _orderNum = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"注单号:%@",_data[@"order"]] size:13 textcolor:ColorHex(0x333333)];
        _orderNum.backgroundColor = ColorHex(0xf5f5f9);
        _orderNum.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:_orderNum];
        
        [_orderNum mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(NAVIBAR_HEIGHT + STATUS_HEIGHT);
            make.centerX.equalTo(self.view);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 38));
        }];
    }
    return _orderNum;
}

- (CYButton *)revokeBtn{
    if (!_revokeBtn) {
        _revokeBtn = [[CYButton alloc]initWithTitle:@"撤销订单" titleColor:ORANGE_COLOR backgroundColor:LINE_COLOR cornerRadius:5];
        _revokeBtn.titleLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightBold];
        [_revokeBtn addTarget:self action:@selector(actionForRevoke) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_revokeBtn];
        
        [_revokeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view.mas_bottom).with.offset( - 20);
            make.centerX.equalTo(self.view);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 40, 44));
        }];
    }
    return _revokeBtn;
}

- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]init];
        _tableview.scrollEnabled = NO;
        [self.view addSubview:_tableview];
        [self setupTableview];
    }
    
    [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_orderNum.mas_bottom).with.offset(0);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - 38));
    }];
    
    return _tableview;
}

- (void)setData:(NSDictionary *)data{
    if (_data != data)
        _data = data;
    self.orderNum.hidden = self.tableview.hidden = NO;
    if ([_data[@"data"][0][4][@"开奖状态:"] intValue] == 2)
        self.revokeBtn.hidden = NO;
    else
        self.revokeBtn.hidden = YES;
}

- (void)setupTableview{
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    WS(self)
    _delegate = [[CYDelegate alloc]initWithCellHeight:44 views:[self footerView] block:nil];
    _datasource = [[CYDataSource alloc]initWithData:_data[@"data"] identifier:@"CYOrderDetailCell" block:^(id cell, id data) {
        [weakSelf bindCell:cell dict:data];
    }];
    _tableview.dataSource = _datasource;
    _tableview.delegate = _delegate;
    
}

- (void)bindCell:(CYOrderDetailCell *)cell dict:(id)dict{
    DeLog(@"dict  %@",dict);
    cell.titleStr = [[dict allKeys] firstObject];
    cell.content = [NSString stringWithFormat:@"%@" ,[[dict allValues] firstObject]];
}

//撤销订单
- (void)actionForRevoke{
    NSDictionary *para = @{@"username":[CYCache readDataWithKey:@"username"],
                           @"token":[CYCache readDataWithKey:@"token"],
                           @"id":@(_bet_id)};
    self.spareTask = [CYHttps POST:API_order_revoke parameters:para showHUD:YES progress:nil success:^(id data) {
        if (data)
            [CYSuccessTipsView showTitle:@"撤单成功" buttonTitle:@"确定" block:^{
                [self performSelector:@selector(popPreviousViewController)];
            }];
    } failure:nil];
    
}

- (void)popPreviousViewController{
    [super popPreviousViewController];
}

- (void)https{
    NSDictionary *para = @{@"username":[CYCache readDataWithKey:@"username"], @"token":[CYCache readDataWithKey:@"token"], @"id":@(_bet_id)};
    
    self.task = [CYHttps POST:API_order_detail parameters:para showHUD:YES progress:nil success:^(id data) {
        DeLog(@"data -- > %@",data);
        if (data)
            [self convertFormat:data];
    } failure:nil];
    
}

//转换格式
- (void)convertFormat:(NSDictionary *)data{
    if ([data count] <=0)
        return;
    NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"cz_name",@"action_no",@"wf_name",@"lottery_no",@"zhuangtai",@"action_data", nil];
    NSMutableArray *array2 = [NSMutableArray arrayWithObjects:@"action_num",@"bet_bonus",@"action_time", nil];
    NSMutableArray *array3 = [NSMutableArray arrayWithObjects:@"彩种:",@"期次:",@"玩法:",@"开奖号码:",@"开奖状态:",@"投注号码:", nil];
    NSMutableArray *array4 = [NSMutableArray arrayWithObjects:@"投注注数:",@"投注金额:",@"投注时间:", nil];
    NSMutableArray *topArr = [NSMutableArray array];//上部分数组--->array1
    NSMutableArray *bottomArr = [NSMutableArray array];//下部分数组---->array2
    
    for (int i = 0; i < array1.count; i ++) {
        [topArr addObject:@{array3[i]:data[array1[i]]}];
    }
    
    for (int i = 0; i < array2.count; i ++) {
        //        if ([array2[i] containsString:@"action_time"]){
        //            NSString * date = data[@"action_no"];
        //            NSUInteger length = [date length];
        //            NSString *new_date = (length <= 8)?date:[date substringToIndex:8];
        //            [bottomArr addObject:@{array4[i]:[NSString stringWithFormat:@"%@%@", new_date, data[array2[i]]]}];
        //        }
        //        else
        [bottomArr addObject:@{array4[i]:data[array2[i]]}];
    }
    
    self.data = @{ @"order":data[@"order"],@"data":@[topArr, bottomArr]};
}

- (NSArray *)footerView{
    return @[@{@"footerView":[[CYView alloc]initWithColor:ColorHex(0xf5f5f9)], @"footerHeight":@(5)}, @{}];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self https];
    
}


- (void)setupNavigationBarTintColor:(UIColor *)color{
    [super setupNavigationBarTintColor:color];
    self.navigationItem.title = @"投注详情";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
